package com.heima.article.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApCollectionMapper;
import com.heima.article.service.ApCollectionService;
import com.heima.common.redis.CacheService;
import com.heima.model.article.dtos.ArticleCollectionDto;
import com.heima.model.article.pojos.ApCollection;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.thread.AppThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
* @author 马毅爽
* @description 针对表【ap_collection(APP收藏信息表)】的数据库操作Service实现
* @createDate 2023-12-19 17:45:37
*/
@Service
public class ApCollectionServiceImpl extends ServiceImpl<ApCollectionMapper, ApCollection>
    implements ApCollectionService{

    @Autowired
    private CacheService cacheService;

    @Override
    public ResponseResult collection(ArticleCollectionDto dto) {
        // 1. 检查参数
        if (dto == null || dto.getEntryId() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 2. 是否登录
        ApUser apUser = AppThreadLocalUtil.getUser();
        if (apUser == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        // 3. 保存用户行为到redis
        String key = "COLLECTION-BEHAVIOR-" + dto.getEntryId().toString();
        String hashKey = apUser.getId().toString();

        if (dto.getOperation() == 0){
            Object object = cacheService.hGet(key, hashKey);
            if (object != null){
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"禁止重复操作");
            }
            cacheService.hPut(key, hashKey, JSON.toJSONString(dto));

            // 4. 保存收藏数据到数据库
            ApCollection one = lambdaQuery()
                    .eq(ApCollection::getEntryId, apUser.getId())
                    .eq(ApCollection::getArticleId, dto.getEntryId())
                    .one();
            if (one != null){
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"禁止重复操作");
            }
            ApCollection apCollection = new ApCollection();
            apCollection.setArticleId(dto.getEntryId());
            apCollection.setEntryId(apUser.getId());
            apCollection.setType(Integer.valueOf(dto.getType()));
            apCollection.setCollectionTime(new Date());
            apCollection.setPublishedTime(dto.getPublishedTime());
            save(apCollection);

        }else {
            cacheService.hDelete(key, hashKey);

            remove(new LambdaQueryWrapper<ApCollection>()
                    .eq(ApCollection::getEntryId, apUser.getId())
                    .eq(ApCollection::getArticleId, dto.getEntryId())
            );
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}




